//
//  MyViewController.m
//  哎游
//
//  Created by 林程杰 on 15/1/8.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "MyViewController.h"
#import "DWBubbleMenuButton.h"
#import "LoginViewCnntroller.h"
#import <ShareSDK/ShareSDK.h>
#import "sinaWiboViewController.h"
#import "MainViewController.h"
#import "Masonry.h"

@implementation MyViewController

- (void)dealloc
{
    [_label release];
    [_scrollView release];
    [super dealloc];
}

- (instancetype) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"我的" style:UIBarButtonItemStyleBordered target:self action:@selector(myLogin :)] autorelease];
    }
    return self;
}

- (void) viewDidLoad
{
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor clearColor]];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, 50, 45)];
    label.textColor = [UIColor whiteColor];
    label.text = @"我的";
    label.font = [UIFont fontWithName:@"TimesNewRomanPS-BoldMT" size:20];
    self.navigationItem.titleView = label;
    
    [label release];

    [self creatView];
  
}

//滚动动画
- (void)viewDidAppear:(BOOL)animated {
   
    UILabel *homeLabel = [self createHomeButtonView];
    
    DWBubbleMenuButton *downMenuButton = [[DWBubbleMenuButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 80, 25.f, homeLabel.frame.size.width * 4,  homeLabel.frame.size.height)  expansionDirection:DirectionDown];
    
    
    
    downMenuButton.homeButtonView = homeLabel;
    
    [downMenuButton addButtons:[self createDemoButtonArray]];
    
    [_scrollView addSubview:downMenuButton];
}



- (void) creatView
{
    
    UIImageView *imageBackground = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    imageBackground.userInteractionEnabled = YES; //设置图片的响应者链
    imageBackground.image = [UIImage imageNamed:@"backGroundPage.png"];
    
    //logo
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.image = [UIImage imageNamed:@"travel.png"];
    [imageBackground addSubview:imageView];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(imageBackground.mas_left).offset(self.view.frame.size.width / 2 - 45);
        make.top.equalTo(imageBackground.mas_top).offset(self.view.frame.size.height / 6);
        make.size.mas_equalTo(CGSizeMake(90, 90));
    }];

    //标语
    UILabel *labelCom = [[UILabel alloc] init];
    [labelCom setTextColor:[UIColor whiteColor]];
    [labelCom setText:@"2015年 lanou3g.com 哎游项目组.AiYou."];
    [imageBackground addSubview:labelCom];
    
    [labelCom mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(imageBackground.mas_left).offset(5);
        make.bottom.equalTo(imageBackground.mas_bottom).offset(-120);
    }];

    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height *1.4 / 5 , self.view.frame.size.width, self.view.frame.size.height / 3)];
    _scrollView.directionalLockEnabled = YES;
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.pagingEnabled = YES;
    _scrollView.contentSize = CGSizeMake(self.view.frame.size.width *2, 0);
    [_scrollView setBackgroundColor:[UIColor clearColor]];
    [imageBackground addSubview:_scrollView];
    
    [_scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(imageBackground.mas_left).offset(0);
        make.top.equalTo(imageBackground.mas_top).offset(self.view.frame.size.height / 3);
        make.right.equalTo(imageBackground.mas_right).offset(0);
        make.bottom.equalTo(imageBackground.mas_bottom).offset(-150);
    }];

    
    
    //关于我们
    UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(_scrollView.frame.size.width + 70, 40, 52, 30)];
    [label1 setText:@"康守帅"];
    [label1 setTextColor:[UIColor grayColor]];
      [_scrollView addSubview:label1];
    UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(_scrollView.frame.size.width + 70, 100, 52, 30)];
    [label2 setText:@"刘建镇"];
    [label2 setTextColor:[UIColor grayColor]];
    UILabel *label3 = [[UILabel alloc] initWithFrame:CGRectMake(_scrollView.frame.size.width  + 70, 160, 52, 30)];
    [label3 setText:@"林程杰"];
    [label3 setTextColor:[UIColor grayColor]];
    [_scrollView addSubview:label2];
    [_scrollView addSubview:label3];
    
    UILabel *label11 = [[UILabel alloc] init];
    [label11 setText:@"  20893454@qq.com"];
    [label11 setTextColor:[UIColor grayColor]];
    [_scrollView addSubview:label11];
    [label11 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(label1.mas_right).offset(1);
        make.top.equalTo(_scrollView.mas_top).offset(45);
    }];
    
    UILabel *label22 = [[UILabel alloc] init];
    [label22 setText:@"  13055770577@163.com"];
    [label22 setTextColor:[UIColor grayColor]];
     [_scrollView addSubview:label22];
    [label22 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(label2.mas_right).offset(1);
        make.top.equalTo(label11.mas_top).offset(60);
    }];

    UILabel *label33 = [[UILabel alloc] init];
    [label33 setText:@"  linchengjie@sina.com"];
    [label33 setTextColor:[UIColor grayColor]];
    [_scrollView addSubview:label33];
    [label33 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(label3.mas_right).offset(1);
        make.top.equalTo(label22.mas_top).offset(60);
    }];

    UIImageView *imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(_scrollView.frame.size.width + 20, 30, 40, 40)];
    imageView1.layer.cornerRadius = 20;
    imageView1.clipsToBounds = YES;
    imageView1.image = [UIImage imageNamed:@"people1.png"];
    
    
   UIImageView *imageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(_scrollView.frame.size.width + 20, 90, 40, 40)];
    imageView2.layer.cornerRadius = 20;
    imageView2.clipsToBounds = YES;
    imageView2.image = [UIImage imageNamed:@"people2.png"];
    
  UIImageView *imageView3 = [[UIImageView alloc] initWithFrame:CGRectMake(_scrollView.frame.size.width + 20, 155, 40, 40)];
    imageView3.layer.cornerRadius = 20;
    imageView3.clipsToBounds = YES;
    imageView3.image = [UIImage imageNamed:@"people3.png"];

   
    [_scrollView addSubview:imageView1];
    [_scrollView addSubview:imageView2];
    [_scrollView addSubview:imageView3];
    

    
    [self.view addSubview:imageBackground];
   

    [imageBackground release];
    [imageView release];
    [label1 release];
    [label11 release];
    [label2 release];
    [label22 release];
    [label3 release];
    [label33 release];
    [imageView1 release];
    [imageView2 release];
    [imageView3 release];
    [labelCom release];
}

//设置tap的样式
- (UILabel *)createHomeButtonView {
    
    //tap和长条label对应的宽高
    _label.text = @"哎";
    _label.textColor = [UIColor whiteColor];
    _label.textAlignment = NSTextAlignmentCenter;
    _label.layer.cornerRadius = _label.frame.size.height / 2.f;
    _label.backgroundColor =[UIColor colorWithRed:0.f green:0.f blue:0.f alpha:0.3f];
    _label.clipsToBounds = YES;

    
    return _label;
}

- (NSArray *)createDemoButtonArray {
    NSMutableArray *buttonsMutable = [[NSMutableArray alloc] init];
    


    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem];
    [button1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button1 setTitle:@"立即登陆" forState:UIControlStateNormal];
    button1.frame = CGRectMake(0.f, 0.f, 120.f, 30.f);
    button1.layer.cornerRadius = button1.frame.size.height / 2.f;
    button1.backgroundColor = [UIColor colorWithRed:0.f green:0.f blue:0.f alpha:0.2f];
    button1.clipsToBounds = YES;
    [button1 addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeSystem];
    [button2 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button2 setTitle:@"游客登录" forState:UIControlStateNormal];
    button2.frame = CGRectMake(0.f, 0.f, 120.f, 30.f);
    button2.layer.cornerRadius = button2.frame.size.height / 2.f;
    button2.backgroundColor = [UIColor colorWithRed:0.f green:0.f blue:0.f alpha:0.2f];
    button2.clipsToBounds = YES;
    [button2 addTarget:self action:@selector(travel) forControlEvents:UIControlEventTouchUpInside];
    
    
    UIButton *button3 = [UIButton buttonWithType:UIButtonTypeSystem];
    [button3 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button3 setTitle:@"关于我们" forState:UIControlStateNormal];
    
    button3.frame = CGRectMake(0.f, 0.f, 120.f, 30.f);
    button3.layer.cornerRadius = button3.frame.size.height / 2.f;
    button3.backgroundColor = [UIColor colorWithRed:0.f green:0.f blue:0.f alpha:0.2f];
    button3.clipsToBounds = YES;
    [button3 addTarget:self action:@selector(about) forControlEvents:UIControlEventTouchUpInside];
    
    

        
        
    [buttonsMutable addObject:button1];
    [buttonsMutable addObject:button2];
    [buttonsMutable addObject:button3];
    
    [button1 release];
    [button2 release];
    [button3 release];
    
    return [buttonsMutable copy];
}

- (void)login {
        [NSTimer scheduledTimerWithTimeInterval:0.8 target:self selector:@selector(performDismiss :) userInfo:nil repeats:NO];
    
}
//用定时器模态
- (void) performDismiss : (NSTimer *) timer{
    LoginViewCnntroller *login = [[LoginViewCnntroller alloc] init];
    UINavigationController *na = [[UINavigationController alloc] initWithRootViewController:login];
   
    [self presentViewController:na animated:YES completion:nil];

    [login release];
    [na release];
}

//游客登录
- (void) travel
{
    MainViewController *main = [[MainViewController alloc] init];
    UITabBarController *tabBar = (UITabBarController *)[[UIApplication sharedApplication].delegate window].rootViewController;
    
    tabBar.selectedIndex = 0;
    
    [main release];
}

- (void) about {

    [UIScrollView beginAnimations:@"自我介绍" context:nil];
    [UIScrollView setAnimationDuration:0.5];
    [UIScrollView setAnimationRepeatAutoreverses:NO];
    [UIScrollView setAnimationRepeatCount:1];
    _scrollView.contentOffset = CGPointMake(_scrollView.frame.size.width, 0);
    [UIScrollView commitAnimations];
}

//个人界面
- (void)myLogin:(id)sender
{
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *str = [user objectForKey:@"login"];
    
    if ([str isEqualToString:@"yes"]) {
        sinaWiboViewController *sina = [[sinaWiboViewController alloc] init];
        UINavigationController *na = [[UINavigationController alloc] initWithRootViewController:sina];
        [self presentViewController:na animated:YES completion:nil];
        [sina release];
        [na release];
    }else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"不好意思" message:@"请先登录哦~" delegate:self cancelButtonTitle:@"登陆" otherButtonTitles:@"取消", nil];
        [alert show];
    }
}

//alert协议方法
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    
    if (buttonIndex == 0) {
        LoginViewCnntroller *login = [[LoginViewCnntroller alloc] init];
        [self presentViewController:login animated:YES completion:nil];
        login.scroll.contentOffset = CGPointMake(login.scroll.frame.size.width, 0);
        [login release];
    }
}


@end
